<template>
	<view class="container"  :class="'theme-'+theme">
	<view class="addTitle">
      <view class="titleDetail">
        <view class="titleLeft" > 
			<image src="../../static/newh5/fanhui.png" 
			class="yticon" 
			style="width: 13px;height: 20px;color:black" 
			@click="returnIndex()"
			></image>
        </view>
        <view class="titleCenter" >
          <b class="titleText">我的收藏</b>
        </view>
        <view class="titleRight"  style="color: #269cd3;flex:1" > </view>
       
      </view>
    </view>
		<view class="f-list">
			<view class="f-item" v-for="(item,index) in favoriteList" :key="index" @click="goToDetail(item)">
				<image class="left" :src="item.thumbnail"></image>
				<view class="right">
					<text class="title more-t">{{item.name}}</text>
					<view class="money-box">
						<text  class="price">{{'￥'+item.price}}</text>
					</view>
					<view class="action">
						<text class="delete" @click.stop="removeOne(item,index)">取消收藏</text>
						<image @click.stop="addCart(item)" src="../../static/add-cart.png"></image>
					</view>
				</view>
			</view>
		</view>
		<uni-load-more :status="loadingType"></uni-load-more>
	</view>
</template>

<script>
	import {  goback } from "@/common/utils/navigateBack.js";
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import {
		getFavoriteList,
		addListToCart,
		removeList,
		addOneToCart
	} from '../../api/favorite/favorite.js'
	import {
		removeCollection
	} from '../../api/product/list.js'
	import {qryConfByItemCode} from '@/api/user/user.js'
	import {queryGoodsLowestPriceById} from '@/api/order/order.js'
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				page: 1,
				favoriteList: [],
				loadingType: 'more',
				// 广州银行-校验商品最低价
				isbuyMinPrice:false
			};
		},
		onReachBottom() {
			this.loadData()
		},
		onLoad() {
			this.loadData()
			this.getConf()
		},
		methods: {
			getConf(){
				const deptInfo = uni.getStorageSync('deptinfo')[0]
				qryConfByItemCode({code: "isbuyMinPrice",
					tenant_id: deptInfo.tenantId,
					dept_code: deptInfo.deptCode}).then(res=>{
						if(res.code===0){
							this.isbuyMinPrice = res.data.val == '1'
						}
				})
			},
			returnIndex() {
				goback()
			},
			loadData() {
				if (this.loadingType === 'loading') {
					return;
				}
				this.loadingType = 'loading'
				const params = {
					page: this.page,
					limit: 10,
					areaId: uni.getStorageSync('areaId')
				}
				getFavoriteList(params).then(res => {
					if (res.code === 0) {
						this.favoriteList = this.favoriteList.concat(res.data.goodsList.records[0])
						this.page += 1;
						if (this.favoriteList.length === parseInt(res.data.goodsList.total)) {
							this.loadingType = 'noMore'
						} else {
							this.loadingType = 'more'
						}
					} else {
						this.$util.msg(res.message)
					}
				})
			},
			removeOne(item, index) {
				const param = {
					product_id: item.id
				}
				removeCollection(param).then(res => {
					if (res.code === 0) {
						this.$util.msg('取消收藏成功')
						this.favoriteList.splice(index, 1)
					} else {
						this.$util.msg(res.message)
					}
				})
			},
			batchRemove() {
				const params = {

				}
				removeList(params).then(res => {

				})
			},
			addCart(item,isMin=false) {
				if(this.isbuyMinPrice&&!isMin){
					this.queryLowestPriceGoods(item)
					return
				}
				var areaId = uni.getStorageSync('areaId')?uni.getStorageSync('areaId'):uni.getStorageSync('enterpriseInfo').areaId;
				var data = {
					goodsId: item.id,
					// areaId: uni.getStorageSync('areaId'),
					areaId:areaId,
					num: 1
				}
				addOneToCart(data).then(res => {
					if (res.code === 0) {
						this.$util.msg('加入购物车成功')
					} else {
						this.$util.msg(res.message)
					}
				})
			},
			queryLowestPriceGoods(item){
				queryGoodsLowestPriceById({goodsId: item.id,purchaserId: uni.getStorageSync('deptinfo')[0].deptId}).then(response=>{
					if(response.code === 0){
					  let res = response.data;
					  if(res.goodsId) {
						  // 返回商品
						  let lowestPrice = Number(res.goodsPrice)
						  if (lowestPrice < item.price){
							//不是最低价商品，需要跳转到新商品
							uni.showModal({
								title:'温馨提示',
								content:'商城存在价格更低的同类商品',
								confirmText:'前往购买',
								showCancel:false,
								success(suc) {
									if(suc.confirm){
										uni.navigateTo({
											url:`/pages/product/product?id=${res.goodsId}`
										})
									}
								}
							})
						  } else {
							this.addCart(item,true)
						  }
					  } else {
						this.addCart(item,true)
					  }
					}else{
						this.addCart(item,true)
					}
				})
			},
			batchAddToCart() {
				const params = {

				}
				addListToCart(params).then(res => {

				})
			},
			goToDetail(item) {
				let id = item.id;
				uni.navigateTo({
					url: `/pages/product/product?id=${id}`
				})
			},
		}
	}
</script>

<style lang="scss">
	page,
	.container {
		background: #f5f5f5;
	}

	.f-list {
		margin-top: 16rpx;
		display: flex;
		flex-direction: column;
		border-radius: 20px;
		background-color: #fff;

		.f-item {
			display: flex;
			padding: 16rpx 30rpx;
			flex-direction: row;

			.left {
				width: 200rpx;
				height: 200rpx;
				border-radius: 10rpx;
			}

			.right {
				flex: 1;
				margin-left: 20rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.title {
					height: 40px;
					text-align: left;
					color: $font-color-dark;
					font-size: 15px;
					// margin-bottom: 8rpx;
				}

				.money-box {
					text-align: left;

					.integral {
						display: inline-block;
						color: $uni-color-primary;
						font-size: 15px;

						&:before {
							background: url(../../static/money.png) no-repeat;
							width: 22rpx;
							height: 22rpx;
							margin-right: 4rpx;
							display: inline-block;
							content: '';
							background-size: contain;
						}

						&:after {
							margin-left: 4rpx;
							content: '积分';
							color: #909090;
							font-size: 12px;
						}
					}

					.split {
						font-size: 15px;
						color: $base-color;
					}

					.price {
            display: inline-block;
            color: $uni-color-primary;
            font-size: 15px;
					}
				}

				.action {
					display: flex;
					flex-direction: row;
					justify-content: flex-end;
					align-items: center;

					.delete {
						text-align: center;
						padding: 4rpx 16rpx;
						font-size: 13px;
						border-radius: 10px;
						background-color: #fff;
						color: $font-color-base;
						border: 0.5px solid #C0C4CC;
					}

					image {
						margin-left: 20rpx;
						width: 60rpx;
						height: 60rpx;
					}
				}
			}
		}
	}
</style>
